/*
 * Display.java
 *
 * Created on April 17, 2007, 9:23 PM
 */

package prj3.gui;

import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;

import javax.swing.SpinnerDateModel;
import javax.swing.SpinnerNumberModel;

import prj3.MasterControl;
import prj3.QueryObject;
import prj3.UpdateObject;
import prj3.BufferedEarthCell;
import prj3.SphericalCoordinate;

/**
 *
 * @author  cwhalen
 */
public class Display extends javax.swing.JFrame {

	private EarthView earthview = new EarthView();

	private SunView sunview = new SunView();

	private boolean sim_running = false;

	/** Creates new form Display */
	public Display() {
		this.setNativeLookAndFeel();
		this.initComponents();
		this.resetDateSpinners();
		this.addViews();
	}
	
	public int getDisplayRate(){
		return this.jSlider_DisplayRate.getValue();
	}

	public void updateGUI(UpdateObject o) {
		this.jTextField_Date.setText(o.simTimeStep.getTime().toString());
		if(this.jCheckBox_Option6.isSelected()) {
			earthview.updateView(o.curListOfCells, o.sunLat, o.sunAngle);
			sunview.updateView(o.sunAngle, o.sunOrbitAngle, o.sunDistance);
		}
	}

	public void updateGUI(QueryObject o) {
		 StringBuffer result = new StringBuffer("\n\n--- [EarthSim2 Query] -------------------------------------------\n");
		
		if(this.jCheckBox_Option1.isSelected()){
			result.append( "Minimum Temperature occured at " + o.minTempTime.getTime().toString() +
				" on ( " + util.Conversions.truncate(o.minTempLat, 2) + ", " + util.Conversions.truncate(o.minTempLong, 2) + "): "+util.Conversions.truncate(o.minTemp, 2)+"\n");
		}
		if(this.jCheckBox_Option4.isSelected()){
			result.append("Maximum Temperature occured at " + o.maxTempTime.getTime().toString() +
			" on ( " + util.Conversions.truncate(o.maxTempLat, 2) + ", " + util.Conversions.truncate(o.maxTempLong, 2) + "): "+util.Conversions.truncate(o.maxTemp, 2)+"\n");
		}
		
		if(this.jCheckBox_Option2.isSelected()){
			result.append("\n\n-- Mean temperatures for regions --\n\n");
			java.util.Iterator it = o.avgForRegion.iterator();
			while(it.hasNext()) {
				BufferedEarthCell cell = (BufferedEarthCell)it.next();
				float value = ((Float)it.next()).floatValue();
				result.append("\t For (" + util.Conversions.truncate(util.Conversions.radToDeg(((SphericalCoordinate)cell.getCoordinate()).getTheta()), 2) +
					", " + util.Conversions.truncate(util.Conversions.radToDeg(((SphericalCoordinate)cell.getCoordinate()).getPhi()), 2) +
					"): " + util.Conversions.truncate(value, 2) + "\n");
			}
		}
		
		if(this.jCheckBox_Option5.isSelected()){
			result.append("\n\n-- Mean temperatures for times --\n\n");
			java.util.Iterator it = o.avgForTimes.iterator();
			while(it.hasNext()) {
				GregorianCalendar time = (GregorianCalendar)it.next();
				float value = ((Float)it.next()).floatValue();
				result.append("\t For " + time.getTime().toString() + ": " + util.Conversions.truncate(value, 2) + "\n");
			}
		}
		
		if(this.jCheckBox_Option3.isSelected()){
			result.append("\n\n-- Actual values at end of simulation --\n\n");
			java.util.Iterator it = o.avgForRegion.iterator();
			while(it.hasNext()) {
				BufferedEarthCell cell = (BufferedEarthCell)it.next();
				it.next();
				float value = cell.getValue();
				result.append("\t For (" + util.Conversions.truncate(util.Conversions.radToDeg(((SphericalCoordinate)cell.getCoordinate()).getTheta()), 2) +
				", " + util.Conversions.truncate(util.Conversions.radToDeg(((SphericalCoordinate)cell.getCoordinate()).getPhi()), 2) +
				"): " + util.Conversions.truncate(value, 2) + "\n");
			}
		}
		
		this.jTextArea_Results.setText(result.toString());
		// Pretend the stop button was pressed
		fresh = true;
		this.sim_running = false;
		this.jButton_Start.setText("Start");
		this.lockControls(false);
	}
	
	/**
	 * 
	 */
	private void resetDateSpinners() {
		Calendar calendar = Calendar.getInstance();
		calendar.set(2000, 2, 21, 12, 1);
		Date start_initDate = calendar.getTime();
		calendar.set(2000, 2, 21, 12, 1);
		Date start_earliestDate = calendar.getTime();
		calendar.set(4000, 2, 21, 0, 0);
		Date start_latestDate = calendar.getTime();
		
		this.jSpinner_BeginDate.setModel(new SpinnerDateModel(start_initDate, start_earliestDate, start_latestDate, Calendar.MINUTE));
		
		Calendar calendar2 = Calendar.getInstance();
		calendar2.set(2001, 2, 21, 12, 1);
		Date end_initDate = calendar2.getTime();
		calendar2.set(2000, 2, 21, 12, 1);
		Date end_earliestDate = calendar2.getTime();
		calendar2.set(4000, 2, 21, 0, 0);
		Date end_latestDate = calendar2.getTime();
		
		this.jSpinner_EndDate.setModel(new SpinnerDateModel(end_initDate, end_earliestDate, end_latestDate, Calendar.MINUTE));
	}

	/**
	 * 
	 */
	private void setNativeLookAndFeel() {
		try {
			javax.swing.UIManager.setLookAndFeel(javax.swing.UIManager
					.getSystemLookAndFeelClassName());
		} catch (Exception e) {
			System.err.println("Error setting native LAF: " + e);
		}
	}

	/**
	 * 
	 */
	private void addViews() {
		earthview.setBounds(15, 10, 740, 380);
		this.jPanel_Sim.add(earthview);
		sunview.setBounds(765, 10, 250, 380);
		this.jPanel_Sim.add(sunview);
	}

	/** This method is called from within the constructor to
	 * initialize the form.
	 * WARNING: Do NOT modify this code. The content of this method is
	 * always regenerated by the Form Editor.
	 */
	// <editor-fold defaultstate="collapsed" desc=" Generated Code ">//GEN-BEGIN:initComponents
	private void initComponents() {
		jPanel_Main = new javax.swing.JPanel();
		jPanel_Sim = new javax.swing.JPanel();
		jSlider_DisplayRate = new javax.swing.JSlider();
		jSlider_ColorOpacity = new javax.swing.JSlider();
		jTextField_Date = new javax.swing.JTextField();
		jLabel_DisplayRate = new javax.swing.JLabel();
		jLabel_ColorOpacity = new javax.swing.JLabel();
		jLabel_PerMS = new javax.swing.JLabel();
		jLabel_PerSec = new javax.swing.JLabel();
		jLabel_Clear = new javax.swing.JLabel();
		jLabel_Opaque = new javax.swing.JLabel();
		jPanel2 = new javax.swing.JPanel();
		jButton_Start = new javax.swing.JButton();
		jButton_Stop = new javax.swing.JButton();
		jButton_Reset = new javax.swing.JButton();
		jPanel_Query = new javax.swing.JPanel();
		jPanel_PhysicalFacotrs = new javax.swing.JPanel();
		jLabel_GridSpacing = new javax.swing.JLabel();
		jSpinner_Grid = new javax.swing.JSpinner();
		jLabel_Tilt = new javax.swing.JLabel();
		jSpinner_Tilt = new javax.swing.JSpinner();
		jSpinner_TimeStep = new javax.swing.JSpinner();
		jLabel_TimeStep = new javax.swing.JLabel();
		jSpinner_Eccentricity = new javax.swing.JSpinner();
		jLabel_Eccentricty = new javax.swing.JLabel();
		jPanel_Calculations = new javax.swing.JPanel();
		jCheckBox_Option1 = new javax.swing.JCheckBox();
		jCheckBox_Option2 = new javax.swing.JCheckBox();
		jCheckBox_Option4 = new javax.swing.JCheckBox();
		jCheckBox_Option3 = new javax.swing.JCheckBox();
		jCheckBox_Option5 = new javax.swing.JCheckBox();
		jCheckBox_Option6 = new javax.swing.JCheckBox();
		jPanel_Ranges = new javax.swing.JPanel();
		jSpinner_BeginDate = new javax.swing.JSpinner();
		jSpinner_EndDate = new javax.swing.JSpinner();
		jLabel1 = new javax.swing.JLabel();
		jSpinner_BeginLattitude = new javax.swing.JSpinner();
		jSpinner_EndLattitude = new javax.swing.JSpinner();
		jSpinner_BeginLongitude = new javax.swing.JSpinner();
		jSpinner_EndLongitude = new javax.swing.JSpinner();
		jLabel_Lattitudes = new javax.swing.JLabel();
		jLabel_Longitudes = new javax.swing.JLabel();
		jLabel_Dates = new javax.swing.JLabel();
		jRadioButton_LattInner = new javax.swing.JRadioButton();
		jRadioButton_LattOutter_R = new javax.swing.JRadioButton();
		jRadioButton_LattOutter_L = new javax.swing.JRadioButton();
		jRadioButton_LongOutter_L = new javax.swing.JRadioButton();
		jRadioButton_LongOutter_R = new javax.swing.JRadioButton();
		jLabel_Inner1 = new javax.swing.JLabel();
		jLabel_Inner2 = new javax.swing.JLabel();
		jRadioButton_LongInner = new javax.swing.JRadioButton();
		jScrollPane_Results = new javax.swing.JScrollPane();
		jTextArea_Results = new javax.swing.JTextArea();
		jMenuBar = new javax.swing.JMenuBar();
		jMenu_File = new javax.swing.JMenu();
		jMenuItem1 = new javax.swing.JMenuItem();

		setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
		setTitle("EarthSim - Design 3");
		setResizable(false);
		jPanel_Main.setLayout(null);

		jPanel_Main.setBackground(new java.awt.Color(255, 255, 255));
		jPanel_Main.setMaximumSize(new java.awt.Dimension(1024, 786));
		jPanel_Main.setMinimumSize(new java.awt.Dimension(1024, 786));
		jPanel_Main.setPreferredSize(new java.awt.Dimension(1024, 786));
		jPanel_Sim.setLayout(null);

		jPanel_Sim.setBorder(new javax.swing.border.EtchedBorder());
		jPanel_Sim.setMaximumSize(new java.awt.Dimension(1024, 470));
		jPanel_Sim.setMinimumSize(new java.awt.Dimension(1024, 470));
		jPanel_Sim.setOpaque(false);
		jPanel_Sim.setPreferredSize(new java.awt.Dimension(1024, 470));
		jSlider_DisplayRate.setBackground(new java.awt.Color(204, 204, 204));
		jSlider_DisplayRate.setMaximum(1000);
		jSlider_DisplayRate.setValue(1000);
		jSlider_DisplayRate.setFocusable(false);
		jSlider_DisplayRate.setInverted(true);
		jSlider_DisplayRate.setOpaque(false);
		jPanel_Sim.add(jSlider_DisplayRate);
		jSlider_DisplayRate.setBounds(30, 420, 350, 25);

		jSlider_ColorOpacity.setFocusable(false);
		jSlider_ColorOpacity.setOpaque(false);
		jSlider_ColorOpacity
				.addChangeListener(new javax.swing.event.ChangeListener() {
					public void stateChanged(javax.swing.event.ChangeEvent evt) {
						jSlider_ColorOpacityStateChanged(evt);
					}
				});

		jPanel_Sim.add(jSlider_ColorOpacity);
		jSlider_ColorOpacity.setBounds(400, 420, 350, 25);

		jPanel_Sim.add(jTextField_Date);
		jTextField_Date.setBounds(770, 400, 240, 19);
		jTextField_Date.setEditable(false);

		jLabel_DisplayRate.setText("Display Rate:");
		jPanel_Sim.add(jLabel_DisplayRate);
		jLabel_DisplayRate.setBounds(30, 400, 64, 14);

		jLabel_ColorOpacity.setText("Color Opacity:");
		jPanel_Sim.add(jLabel_ColorOpacity);
		jLabel_ColorOpacity.setBounds(400, 400, 69, 14);

		jLabel_PerMS.setText("per ms");
		jPanel_Sim.add(jLabel_PerMS);
		jLabel_PerMS.setBounds(340, 440, 50, 14);

		jLabel_PerSec.setText("per sec");
		jPanel_Sim.add(jLabel_PerSec);
		jLabel_PerSec.setBounds(40, 440, 50, 14);

		jLabel_Clear.setText("clear");
		jPanel_Sim.add(jLabel_Clear);
		jLabel_Clear.setBounds(410, 440, 50, 14);

		jLabel_Opaque.setText("opaque");
		jPanel_Sim.add(jLabel_Opaque);
		jLabel_Opaque.setBounds(710, 440, 36, 14);

		jPanel2.setLayout(null);

		jPanel2.setBackground(new java.awt.Color(255, 255, 255));
		jButton_Start.setText("Start");
		jButton_Start.addActionListener(new java.awt.event.ActionListener() {
			public void actionPerformed(java.awt.event.ActionEvent evt) {
				jButton_StartActionPerformed(evt);
			}
		});

		jPanel2.add(jButton_Start);
		jButton_Start.setBounds(10, 10, 70, 23);

		jButton_Stop.setText("Stop");
		jButton_Stop.addActionListener(new java.awt.event.ActionListener() {
			public void actionPerformed(java.awt.event.ActionEvent evt) {
				jButton_StopActionPerformed(evt);
			}
		});

		jPanel2.add(jButton_Stop);
		jButton_Stop.setBounds(100, 10, 60, 23);

		jButton_Reset.setText("Reset");
		jButton_Reset.addActionListener(new java.awt.event.ActionListener() {
			public void actionPerformed(java.awt.event.ActionEvent evt) {
				jButton_ResetActionPerformed(evt);
			}
		});

		jPanel2.add(jButton_Reset);
		jButton_Reset.setBounds(180, 10, 70, 23);

		jPanel_Sim.add(jPanel2);
		jPanel2.setBounds(760, 420, 260, 40);

		jPanel_Main.add(jPanel_Sim);
		jPanel_Sim.setBounds(0, 0, 1024, 470);

		jPanel_Query.setLayout(null);

		jPanel_Query.setBackground(new java.awt.Color(255, 255, 255));
		jPanel_Query.setMaximumSize(new java.awt.Dimension(1024, 32767));
		jPanel_Query.setMinimumSize(new java.awt.Dimension(1024, 10));
		jPanel_PhysicalFacotrs.setLayout(null);

		jPanel_PhysicalFacotrs.setBackground(new java.awt.Color(255, 255, 255));
		jPanel_PhysicalFacotrs.setBorder(new javax.swing.border.TitledBorder(
				"Physical Factors"));
		jLabel_GridSpacing.setText("Grid Spacing:");
		jPanel_PhysicalFacotrs.add(jLabel_GridSpacing);
		jLabel_GridSpacing.setBounds(10, 20, 80, 14);

		jSpinner_Grid.setModel(new SpinnerNumberModel(
				Constants.DEFAULT_GRID_SPACING, Constants.MIN_GRID_SPACING,
				Constants.MAX_GRID_SPACING, Constants.STEP_GRID_SPACING));
		jPanel_PhysicalFacotrs.add(jSpinner_Grid);
		jSpinner_Grid.setBounds(10, 40, 80, 18);

		jLabel_Tilt.setText("Axial Tilt \u00ba: ");
		jPanel_PhysicalFacotrs.add(jLabel_Tilt);
		jLabel_Tilt.setBounds(190, 20, 80, 14);

		jSpinner_Tilt.setModel(new SpinnerNumberModel(
				Constants.DEFAULT_AXIAL_TILT, Constants.MIN_AXIAL_TILT,
				Constants.MAX_AXIAL_TILT, Constants.STEP_AXIAL_TILT));
		jPanel_PhysicalFacotrs.add(jSpinner_Tilt);
		jSpinner_Tilt.setBounds(190, 40, 80, 18);

		jSpinner_TimeStep.setModel(new SpinnerNumberModel(
				Constants.DEFAULT_SIM_TIME_STEP, Constants.MIN_SIM_TIME_STEP,
				Constants.MAX_SIM_TIME_STEP, Constants.STEP_SIM_TIME_STEP));
		jPanel_PhysicalFacotrs.add(jSpinner_TimeStep);
		jSpinner_TimeStep.setBounds(100, 40, 80, 18);

		jLabel_TimeStep.setText("Time Step:");
		jPanel_PhysicalFacotrs.add(jLabel_TimeStep);
		jLabel_TimeStep.setBounds(100, 20, 80, 14);

		jSpinner_Eccentricity.setModel(new SpinnerNumberModel(
				Constants.DEFAULT_ECCENTRICITY, Constants.MIN_ECCENTRICITY,
				Constants.MAX_ECCENTRICITY, Constants.STEP_ECCENTRICITY));
		jPanel_PhysicalFacotrs.add(jSpinner_Eccentricity);
		jSpinner_Eccentricity.setBounds(280, 40, 80, 18);

		jLabel_Eccentricty.setText("Eccentricity:");
		jPanel_PhysicalFacotrs.add(jLabel_Eccentricty);
		jLabel_Eccentricty.setBounds(280, 20, 80, 14);

		jPanel_Query.add(jPanel_PhysicalFacotrs);
		jPanel_PhysicalFacotrs.setBounds(640, 10, 370, 70);

		jPanel_Calculations.setLayout(null);

		jPanel_Calculations.setBackground(new java.awt.Color(255, 255, 255));
		jPanel_Calculations.setBorder(new javax.swing.border.TitledBorder(
				"Calculations"));
		jCheckBox_Option1.setText("Minimum Temperature (Region)");
		jCheckBox_Option1.setActionCommand("Minimum temperature in the region");
		jCheckBox_Option1.setFocusable(false);
		jCheckBox_Option1.setOpaque(false);
		jPanel_Calculations.add(jCheckBox_Option1);
		jCheckBox_Option1.setBounds(10, 20, 180, 23);

		jCheckBox_Option2.setText("Mean Temperature (For Region)");
		jCheckBox_Option2.setFocusable(false);
		jCheckBox_Option2.setOpaque(false);
		jPanel_Calculations.add(jCheckBox_Option2);
		jCheckBox_Option2.setBounds(10, 40, 180, 23);

		jCheckBox_Option4.setText("Maximum Temperature (Region)");
		jCheckBox_Option4.setFocusable(false);
		jCheckBox_Option4.setOpaque(false);
		jPanel_Calculations.add(jCheckBox_Option4);
		jCheckBox_Option4.setBounds(190, 20, 180, 23);

		jCheckBox_Option3.setText("Actual Cell Values");
		jCheckBox_Option3.setFocusable(false);
		jCheckBox_Option3.setOpaque(false);
		jPanel_Calculations.add(jCheckBox_Option3);
		jCheckBox_Option3.setBounds(10, 60, 160, 23);

		jCheckBox_Option5.setText("Mean Temperature (For Times)");
		jCheckBox_Option5.setFocusable(false);
		jCheckBox_Option5.setOpaque(false);
		jPanel_Calculations.add(jCheckBox_Option5);
		jCheckBox_Option5.setBounds(190, 40, 180, 23);

		jCheckBox_Option6.setSelected(true);
		jCheckBox_Option6.setText("Display Simulation");
		jCheckBox_Option6.setFocusable(false);
		jCheckBox_Option6.setOpaque(false);
		jPanel_Calculations.add(jCheckBox_Option6);
		jCheckBox_Option6.setBounds(190, 60, 150, 23);

		jPanel_Query.add(jPanel_Calculations);
		jPanel_Calculations.setBounds(640, 220, 370, 90);

		jPanel_Ranges.setLayout(null);

		jPanel_Ranges.setBackground(new java.awt.Color(255, 255, 255));
		jPanel_Ranges.setBorder(new javax.swing.border.TitledBorder("Ranges"));
		jSpinner_BeginDate.setModel(new SpinnerDateModel());
		jPanel_Ranges.add(jSpinner_BeginDate);
		jSpinner_BeginDate.setBounds(50, 30, 130, 18);

		jSpinner_EndDate.setModel(new SpinnerDateModel());
		jPanel_Ranges.add(jSpinner_EndDate);
		jSpinner_EndDate.setBounds(220, 30, 130, 20);

		jLabel1.setFont(new java.awt.Font("Tahoma", 0, 12));
		jLabel1.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
		jLabel1.setText("to");
		jLabel1.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
		jPanel_Ranges.add(jLabel1);
		jLabel1.setBounds(184, 30, 30, 20);

		jSpinner_BeginLattitude.setModel(new SpinnerNumberModel(
				Constants.MIN_LATTITUDE, Constants.MIN_LATTITUDE,
				Constants.MAX_LATTITUDE, Constants.STEP_GRID_SPACING));
		jPanel_Ranges.add(jSpinner_BeginLattitude);
		jSpinner_BeginLattitude.setBounds(120, 60, 50, 20);

		jSpinner_EndLattitude.setModel(new SpinnerNumberModel(
				Constants.MAX_LATTITUDE, Constants.MIN_LATTITUDE,
				Constants.MAX_LATTITUDE, Constants.STEP_GRID_SPACING));
		jPanel_Ranges.add(jSpinner_EndLattitude);
		jSpinner_EndLattitude.setBounds(250, 60, 50, 20);

		jSpinner_BeginLongitude.setModel(new SpinnerNumberModel(
				Constants.MIN_LONGITUDE, Constants.MIN_LONGITUDE,
				Constants.MAX_LONGITUDE, Constants.STEP_GRID_SPACING));
		jPanel_Ranges.add(jSpinner_BeginLongitude);
		jSpinner_BeginLongitude.setBounds(120, 90, 50, 20);

		jSpinner_EndLongitude.setModel(new SpinnerNumberModel(
				Constants.MAX_LONGITUDE, Constants.MIN_LONGITUDE,
				Constants.MAX_LONGITUDE, Constants.STEP_GRID_SPACING));
		jPanel_Ranges.add(jSpinner_EndLongitude);
		jSpinner_EndLongitude.setBounds(250, 90, 50, 20);

		jLabel_Lattitudes.setText("Lattitudes:");
		jPanel_Ranges.add(jLabel_Lattitudes);
		jLabel_Lattitudes.setBounds(10, 60, 52, 20);

		jLabel_Longitudes.setText("Longitudes:");
		jPanel_Ranges.add(jLabel_Longitudes);
		jLabel_Longitudes.setBounds(10, 90, 56, 20);

		jLabel_Dates.setText("Dates:");
		jPanel_Ranges.add(jLabel_Dates);
		jLabel_Dates.setBounds(10, 30, 34, 20);

		jRadioButton_LattInner.setBackground(new java.awt.Color(255, 255, 255));
		jRadioButton_LattInner.setFont(new java.awt.Font("Arial", 1, 12));
		jRadioButton_LattInner.setSelected(true);
		jRadioButton_LattInner.setText("<=");
		jRadioButton_LattInner.setFocusable(false);
		jRadioButton_LattInner
				.addActionListener(new java.awt.event.ActionListener() {
					public void actionPerformed(java.awt.event.ActionEvent evt) {
						jRadioButton_LattInnerActionPerformed(evt);
					}
				});

		jPanel_Ranges.add(jRadioButton_LattInner);
		jRadioButton_LattInner.setBounds(200, 60, 39, 23);

		jRadioButton_LattOutter_R.setBackground(new java.awt.Color(255, 255,
				255));
		jRadioButton_LattOutter_R.setFont(new java.awt.Font("Arial", 1, 12));
		jRadioButton_LattOutter_R.setText("=>");
		jRadioButton_LattOutter_R.setFocusable(false);
		jRadioButton_LattOutter_R
				.setHorizontalAlignment(javax.swing.SwingConstants.TRAILING);
		jRadioButton_LattOutter_R
				.setHorizontalTextPosition(javax.swing.SwingConstants.LEADING);
		jRadioButton_LattOutter_R
				.addActionListener(new java.awt.event.ActionListener() {
					public void actionPerformed(java.awt.event.ActionEvent evt) {
						jRadioButton_LattOutter_RActionPerformed(evt);
					}
				});

		jPanel_Ranges.add(jRadioButton_LattOutter_R);
		jRadioButton_LattOutter_R.setBounds(301, 60, 50, 23);

		jRadioButton_LattOutter_L.setBackground(new java.awt.Color(255, 255,
				255));
		jRadioButton_LattOutter_L.setFont(new java.awt.Font("Arial", 1, 12));
		jRadioButton_LattOutter_L.setText("<=");
		jRadioButton_LattOutter_L.setFocusable(false);
		jRadioButton_LattOutter_L
				.addActionListener(new java.awt.event.ActionListener() {
					public void actionPerformed(java.awt.event.ActionEvent evt) {
						jRadioButton_LattOutter_LActionPerformed(evt);
					}
				});

		jPanel_Ranges.add(jRadioButton_LattOutter_L);
		jRadioButton_LattOutter_L.setBounds(70, 60, 50, 23);

		jRadioButton_LongOutter_L.setBackground(new java.awt.Color(255, 255,
				255));
		jRadioButton_LongOutter_L.setFont(new java.awt.Font("Arial", 1, 12));
		jRadioButton_LongOutter_L.setText("<=");
		jRadioButton_LongOutter_L.setFocusable(false);
		jRadioButton_LongOutter_L
				.addActionListener(new java.awt.event.ActionListener() {
					public void actionPerformed(java.awt.event.ActionEvent evt) {
						jRadioButton_LongOutter_LActionPerformed(evt);
					}
				});

		jPanel_Ranges.add(jRadioButton_LongOutter_L);
		jRadioButton_LongOutter_L.setBounds(70, 90, 50, 23);

		jRadioButton_LongOutter_R.setBackground(new java.awt.Color(255, 255,
				255));
		jRadioButton_LongOutter_R.setFont(new java.awt.Font("Arial", 1, 12));
		jRadioButton_LongOutter_R.setText("=>");
		jRadioButton_LongOutter_R.setFocusable(false);
		jRadioButton_LongOutter_R
				.setHorizontalAlignment(javax.swing.SwingConstants.TRAILING);
		jRadioButton_LongOutter_R
				.setHorizontalTextPosition(javax.swing.SwingConstants.LEADING);
		jRadioButton_LongOutter_R
				.addActionListener(new java.awt.event.ActionListener() {
					public void actionPerformed(java.awt.event.ActionEvent evt) {
						jRadioButton_LongOutter_RActionPerformed(evt);
					}
				});

		jPanel_Ranges.add(jRadioButton_LongOutter_R);
		jRadioButton_LongOutter_R.setBounds(300, 90, 50, 23);

		jLabel_Inner1.setFont(new java.awt.Font("Arial", 1, 12));
		jLabel_Inner1.setText("=>");
		jPanel_Ranges.add(jLabel_Inner1);
		jLabel_Inner1.setBounds(180, 60, 14, 20);

		jLabel_Inner2.setFont(new java.awt.Font("Arial", 1, 12));
		jLabel_Inner2.setText("=>");
		jPanel_Ranges.add(jLabel_Inner2);
		jLabel_Inner2.setBounds(180, 90, 14, 20);

		jRadioButton_LongInner.setBackground(new java.awt.Color(255, 255, 255));
		jRadioButton_LongInner.setFont(new java.awt.Font("Arial", 1, 12));
		jRadioButton_LongInner.setSelected(true);
		jRadioButton_LongInner.setText("<=");
		jRadioButton_LongInner.setFocusable(false);
		jRadioButton_LongInner
				.addActionListener(new java.awt.event.ActionListener() {
					public void actionPerformed(java.awt.event.ActionEvent evt) {
						jRadioButton_LongInnerActionPerformed(evt);
					}
				});

		jPanel_Ranges.add(jRadioButton_LongInner);
		jRadioButton_LongInner.setBounds(200, 90, 39, 23);

		jPanel_Query.add(jPanel_Ranges);
		jPanel_Ranges.setBounds(640, 90, 370, 120);

		jScrollPane_Results
				.setHorizontalScrollBarPolicy(javax.swing.ScrollPaneConstants.HORIZONTAL_SCROLLBAR_ALWAYS);
		jScrollPane_Results
				.setVerticalScrollBarPolicy(javax.swing.ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
		jTextArea_Results.setBackground(new java.awt.Color(250, 250, 255));
		jScrollPane_Results.setViewportView(jTextArea_Results);

		jPanel_Query.add(jScrollPane_Results);
		jScrollPane_Results.setBounds(12, 12, 620, 290);

		jPanel_Main.add(jPanel_Query);
		jPanel_Query.setBounds(0, 470, 1024, 316);

		getContentPane().add(jPanel_Main, java.awt.BorderLayout.CENTER);

		jMenu_File.setText("File");
		jMenuItem1.setText("Exit");
		jMenu_File.add(jMenuItem1);

		jMenuBar.add(jMenu_File);

		setJMenuBar(jMenuBar);

		pack();
	}

	// </editor-fold>//GEN-END:initComponents

	private void jRadioButton_LongInnerActionPerformed(
			java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jRadioButton_LongInnerActionPerformed
		this.jRadioButton_LongInner.setSelected(true);
		this.jRadioButton_LongOutter_L.setSelected(false);
		this.jRadioButton_LongOutter_R.setSelected(false);
	}//GEN-LAST:event_jRadioButton_LongInnerActionPerformed

	private void jRadioButton_LongOutter_LActionPerformed(
			java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jRadioButton_LongOutter_LActionPerformed
		this.jRadioButton_LongInner.setSelected(false);
		this.jRadioButton_LongOutter_L.setSelected(true);
		this.jRadioButton_LongOutter_R.setSelected(true);
	}//GEN-LAST:event_jRadioButton_LongOutter_LActionPerformed

	private void jRadioButton_LongOutter_RActionPerformed(
			java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jRadioButton_LongOutter_RActionPerformed
		this.jRadioButton_LongInner.setSelected(false);
		this.jRadioButton_LongOutter_L.setSelected(true);
		this.jRadioButton_LongOutter_R.setSelected(true);
	}//GEN-LAST:event_jRadioButton_LongOutter_RActionPerformed

	private void jRadioButton_LattInnerActionPerformed(
			java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jRadioButton_LattInnerActionPerformed
		this.jRadioButton_LattInner.setSelected(true);
		this.jRadioButton_LattOutter_L.setSelected(false);
		this.jRadioButton_LattOutter_R.setSelected(false);
	}//GEN-LAST:event_jRadioButton_LattInnerActionPerformed

	private void jRadioButton_LattOutter_LActionPerformed(
			java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jRadioButton_LattOutter_LActionPerformed
		this.jRadioButton_LattInner.setSelected(false);
		this.jRadioButton_LattOutter_L.setSelected(true);
		this.jRadioButton_LattOutter_R.setSelected(true);
	}//GEN-LAST:event_jRadioButton_LattOutter_LActionPerformed

	private void jRadioButton_LattOutter_RActionPerformed(
			java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jRadioButton_LattOutter_RActionPerformed
		this.jRadioButton_LattInner.setSelected(false);
		this.jRadioButton_LattOutter_L.setSelected(true);
		this.jRadioButton_LattOutter_R.setSelected(true);
	}//GEN-LAST:event_jRadioButton_LattOutter_RActionPerformed

	private void jSlider_ColorOpacityStateChanged(
			javax.swing.event.ChangeEvent evt) {//GEN-FIRST:event_jSlider_ColorOpacityStateChanged
		this.earthview.setColorOpacity(((float) this.jSlider_ColorOpacity.getValue()) / 100.0f);
		this.earthview.repaint();
	}//GEN-LAST:event_jSlider_ColorOpacityStateChanged

	private void jButton_ResetActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton_ResetActionPerformed
		this.jSlider_DisplayRate.setValue(Constants.DEFAULT_DISPLAY_RATE);
		this.jSlider_ColorOpacity.setValue(Constants.DEFAULT_COLOR_OPACITY);

		this.jSpinner_Grid.setModel(new SpinnerNumberModel(
				Constants.DEFAULT_GRID_SPACING, Constants.MIN_GRID_SPACING,
				Constants.MAX_GRID_SPACING, Constants.STEP_GRID_SPACING));
		this.jSpinner_TimeStep.setModel(new SpinnerNumberModel(
				Constants.DEFAULT_SIM_TIME_STEP, Constants.MIN_SIM_TIME_STEP,
				Constants.MAX_SIM_TIME_STEP, Constants.STEP_SIM_TIME_STEP));
		this.jSpinner_Tilt.setModel(new SpinnerNumberModel(
				Constants.DEFAULT_AXIAL_TILT, Constants.MIN_AXIAL_TILT,
				Constants.MAX_AXIAL_TILT, Constants.STEP_AXIAL_TILT));
		this.jSpinner_Eccentricity.setModel(new SpinnerNumberModel(
				Constants.DEFAULT_ECCENTRICITY, Constants.MIN_ECCENTRICITY,
				Constants.MAX_ECCENTRICITY, Constants.STEP_ECCENTRICITY));

		this.resetDateSpinners();

		this.jSpinner_BeginLattitude.setModel(new SpinnerNumberModel(
				Constants.MIN_LATTITUDE, Constants.MIN_LATTITUDE,
				Constants.MAX_LATTITUDE, Constants.STEP_GRID_SPACING));
		this.jSpinner_EndLattitude.setModel(new SpinnerNumberModel(
				Constants.MAX_LATTITUDE, Constants.MIN_LATTITUDE,
				Constants.MAX_LATTITUDE, Constants.STEP_GRID_SPACING));
		this.jSpinner_BeginLongitude.setModel(new SpinnerNumberModel(
				Constants.MIN_LONGITUDE, Constants.MIN_LONGITUDE,
				Constants.MAX_LONGITUDE, Constants.STEP_GRID_SPACING));
		this.jSpinner_EndLongitude.setModel(new SpinnerNumberModel(
				Constants.MAX_LONGITUDE, Constants.MIN_LONGITUDE,
				Constants.MAX_LONGITUDE, Constants.STEP_GRID_SPACING));

		this.jRadioButton_LattInner.setSelected(true);
		this.jRadioButton_LattOutter_L.setSelected(false);
		this.jRadioButton_LattOutter_R.setSelected(false);

		this.jRadioButton_LongInner.setSelected(true);
		this.jRadioButton_LongOutter_L.setSelected(false);
		this.jRadioButton_LongOutter_R.setSelected(false);

		this.jCheckBox_Option1.setSelected(false);
		this.jCheckBox_Option2.setSelected(false);
		this.jCheckBox_Option3.setSelected(false);
		this.jCheckBox_Option4.setSelected(false);
		this.jCheckBox_Option5.setSelected(false);
		this.jCheckBox_Option6.setSelected(true);

		this.earthview.setGridSpacing(Constants.DEFAULT_GRID_SPACING);

	}//GEN-LAST:event_jButton_ResetActionPerformed

	boolean fresh = true;
	
	private void jButton_StopActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton_StopActionPerformed
		fresh = true;
		this.sim_running = false;
		this.jButton_Start.setText("Start");
		this.lockControls(false);
		
		new Thread(new Runnable()
		{
			public void run()
			{
				MasterControl.stopSim();
			}
		}).start();
		
	}//GEN-LAST:event_jButton_StopActionPerformed

	private void jButton_StartActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton_StartActionPerformed
		this.sim_running = (this.sim_running) ? false : true;
		
		this.lockControls(true);
		this.earthview.setGridSpacing(Integer.parseInt(this.jSpinner_Grid.getValue().toString()));
		
		if (!sim_running) {
			this.jButton_Start.setText("Start");
			new Thread(new Runnable()
			{
				public void run()
				{
					MasterControl.pauseSim();
				}
			}).start();
		} else {
			this.jButton_Start.setText("Pause");
			
			if(fresh){
				fresh = false;
				new Thread(new Runnable()
				{
					public void run()
					{
						start();
					}
				}).start();
			} else {
				new Thread(new Runnable()
				{
					public void run()
					{
						MasterControl.restartSim();
					}
				}).start();
			}
		}

	}//GEN-LAST:event_jButton_StartActionPerformed
	
	private void start() {
		boolean getMinTemp = this.jCheckBox_Option1.isSelected();
		boolean getMaxTemp = this.jCheckBox_Option4.isSelected();
		boolean getMeanTempForRegion = this.jCheckBox_Option2.isSelected();this.jCheckBox_Option1.isSelected();
		boolean getMeanTempForTimes = this.jCheckBox_Option5.isSelected();
		double gridStep = Math.toRadians(Double.parseDouble(this.jSpinner_Grid.getValue().toString()));
		int timeStep = Integer.parseInt(this.jSpinner_TimeStep.getValue().toString());
		double tilt = Math.toRadians(Double.parseDouble(this.jSpinner_Tilt.getValue().toString()));
		double eccentricity = Double.parseDouble(this.jSpinner_Eccentricity.getValue().toString());
		
		GregorianCalendar gcStart = new GregorianCalendar();
		GregorianCalendar gcEnd = new GregorianCalendar();
		
		gcStart.setTime((Date)this.jSpinner_BeginDate.getValue());
		GregorianCalendar startDate = gcStart;
		gcEnd.setTime((Date)this.jSpinner_EndDate.getValue());
		GregorianCalendar endDate = gcEnd;
		
		
		double lat1 = Math.toRadians(Double.parseDouble(this.jSpinner_BeginLattitude.getValue().toString()));
		double lat2 = Math.toRadians(Double.parseDouble(this.jSpinner_EndLattitude.getValue().toString()));
		double startLatitude;
		double endLatitude;
		
		if(this.jRadioButton_LattInner.isSelected()){
			startLatitude = (lat1 > lat2)? lat1 : lat2;
			endLatitude = (lat1 > lat2)? lat2 : lat1;
		} else {
			startLatitude = (lat1 > lat2)? lat2 : lat1;
			endLatitude = (lat1 > lat2)? lat1 : lat2;
		}
		
		double lon1 = Math.toRadians(Double.parseDouble(this.jSpinner_BeginLongitude.getValue().toString()));
		double lon2 = Math.toRadians(Double.parseDouble(this.jSpinner_EndLongitude.getValue().toString()));
		double startLongitude;
		double endLongitude;
		
		if(this.jRadioButton_LongInner.isSelected()){
			startLongitude = (lon1 > lon2)? lon1 : lon2;
			endLongitude = (lon1 > lon2)? lon2 : lon1;
		} else {
			startLongitude = (lon1 > lon2)? lon2 : lon1;
			endLongitude = (lon1 > lon2)? lon1 : lon2;
		}
		
		/*System.out.println("-----------------------------");
		System.out.println(getMinTemp);
		System.out.println(getMaxTemp);
		System.out.println(getMeanTempForRegion);
		System.out.println(getMeanTempForTimes);
		System.out.println(gridStep);
		System.out.println(timeStep);
		System.out.println(tilt);
		System.out.println(eccentricity);
		System.out.println(startDate);
		System.out.println(endDate);
		System.out.println(startLatitude);
		System.out.println(endLatitude);
		System.out.println(startLongitude);
		System.out.println(endLongitude);
		System.out.println("-----------------------------");*/
		this.jTextArea_Results.setText("Processing...");
		this.updateGUI(MasterControl.getQueryData(this, getMinTemp, getMaxTemp, getMeanTempForRegion, getMeanTempForTimes, gridStep, timeStep, tilt, eccentricity, startDate, endDate, startLatitude, endLatitude, startLongitude, endLongitude));
	}

	private void lockControls(boolean b) {
		boolean lock = !b;
		this.jSpinner_Grid.setEnabled(lock);
		this.jSpinner_Tilt.setEnabled(lock);
		this.jSpinner_Eccentricity.setEnabled(lock);
		this.jSpinner_TimeStep.setEnabled(lock);
		this.jButton_Reset.setEnabled(lock);

		this.jSpinner_BeginDate.setEnabled(lock);
		this.jSpinner_EndDate.setEnabled(lock);

		this.jRadioButton_LattInner.setEnabled(lock);
		this.jRadioButton_LattOutter_L.setEnabled(lock);
		this.jRadioButton_LattOutter_R.setEnabled(lock);

		this.jRadioButton_LongInner.setEnabled(lock);
		this.jRadioButton_LongOutter_L.setEnabled(lock);
		this.jRadioButton_LongOutter_R.setEnabled(lock);

		this.jSpinner_BeginLattitude.setEnabled(lock);
		this.jSpinner_EndLattitude.setEnabled(lock);
		this.jSpinner_BeginLongitude.setEnabled(lock);
		this.jSpinner_EndLongitude.setEnabled(lock);

		this.jCheckBox_Option1.setEnabled(lock);
		this.jCheckBox_Option2.setEnabled(lock);
		this.jCheckBox_Option3.setEnabled(lock);
		this.jCheckBox_Option4.setEnabled(lock);
		this.jCheckBox_Option5.setEnabled(lock);
		this.jCheckBox_Option6.setEnabled(lock);
	}

	/**
	 * @param args the command line arguments
	 */
	public static void main(String args[]) {
		java.awt.EventQueue.invokeLater(new Runnable() {
			public void run() {
				new Display().setVisible(true);
			}
		});
	}

	// Variables declaration - do not modify//GEN-BEGIN:variables
	private javax.swing.JButton jButton_Reset;

	private javax.swing.JButton jButton_Start;

	private javax.swing.JButton jButton_Stop;

	private javax.swing.JCheckBox jCheckBox_Option1;

	private javax.swing.JCheckBox jCheckBox_Option2;

	private javax.swing.JCheckBox jCheckBox_Option3;

	private javax.swing.JCheckBox jCheckBox_Option4;

	private javax.swing.JCheckBox jCheckBox_Option5;

	private javax.swing.JCheckBox jCheckBox_Option6;

	private javax.swing.JLabel jLabel1;

	private javax.swing.JLabel jLabel_Clear;

	private javax.swing.JLabel jLabel_ColorOpacity;

	private javax.swing.JLabel jLabel_Dates;

	private javax.swing.JLabel jLabel_DisplayRate;

	private javax.swing.JLabel jLabel_Eccentricty;

	private javax.swing.JLabel jLabel_GridSpacing;

	private javax.swing.JLabel jLabel_Inner1;

	private javax.swing.JLabel jLabel_Inner2;

	private javax.swing.JLabel jLabel_Lattitudes;

	private javax.swing.JLabel jLabel_Longitudes;

	private javax.swing.JLabel jLabel_Opaque;

	private javax.swing.JLabel jLabel_PerMS;

	private javax.swing.JLabel jLabel_PerSec;

	private javax.swing.JLabel jLabel_Tilt;

	private javax.swing.JLabel jLabel_TimeStep;

	private javax.swing.JMenuBar jMenuBar;

	private javax.swing.JMenuItem jMenuItem1;

	private javax.swing.JMenu jMenu_File;

	private javax.swing.JPanel jPanel2;

	private javax.swing.JPanel jPanel_Calculations;

	private javax.swing.JPanel jPanel_Main;

	private javax.swing.JPanel jPanel_PhysicalFacotrs;

	private javax.swing.JPanel jPanel_Query;

	private javax.swing.JPanel jPanel_Ranges;

	private javax.swing.JPanel jPanel_Sim;

	private javax.swing.JRadioButton jRadioButton_LattInner;

	private javax.swing.JRadioButton jRadioButton_LattOutter_L;

	private javax.swing.JRadioButton jRadioButton_LattOutter_R;

	private javax.swing.JRadioButton jRadioButton_LongInner;

	private javax.swing.JRadioButton jRadioButton_LongOutter_L;

	private javax.swing.JRadioButton jRadioButton_LongOutter_R;

	private javax.swing.JScrollPane jScrollPane_Results;

	private javax.swing.JSlider jSlider_ColorOpacity;

	private javax.swing.JSlider jSlider_DisplayRate;

	private javax.swing.JSpinner jSpinner_BeginDate;

	private javax.swing.JSpinner jSpinner_BeginLattitude;

	private javax.swing.JSpinner jSpinner_BeginLongitude;

	private javax.swing.JSpinner jSpinner_Eccentricity;

	private javax.swing.JSpinner jSpinner_EndDate;

	private javax.swing.JSpinner jSpinner_EndLattitude;

	private javax.swing.JSpinner jSpinner_EndLongitude;

	private javax.swing.JSpinner jSpinner_Grid;

	private javax.swing.JSpinner jSpinner_Tilt;

	private javax.swing.JSpinner jSpinner_TimeStep;

	private javax.swing.JTextArea jTextArea_Results;

	private javax.swing.JTextField jTextField_Date;
	// End of variables declaration//GEN-END:variables

}
